/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    int solve(TreeNode *root, int &weight) {
        if (root==NULL) {
            weight=0;
            return 0;
        }
        int ret=0;
        weight=0;
        int tmpWeight;
        ret+=solve(root->left, tmpWeight);
        weight+=tmpWeight*10;
        ret+=solve(root->right, tmpWeight);
        weight+=tmpWeight*10;
        if (weight==0) weight=1;
        ret+=root->val*weight;
        return ret;
    }

public:
    int sumNumbers(TreeNode *root) {
        int weight;
        return solve(root, weight);
    }
};
